<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Generate a pseudo-random string of bytes</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-public-encrypt.html">openssl_public_encrypt</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.openssl-seal.html">openssl_seal</a></div>
 <div class="up"><a href="ref.openssl.html">OpenSSL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.openssl-random-pseudo-bytes" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">openssl_random_pseudo_bytes</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">openssl_random_pseudo_bytes</span> &mdash; <span class="dc-title">Generate a pseudo-random string of bytes</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.openssl-random-pseudo-bytes-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>openssl_random_pseudo_bytes</strong></span>
    ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter reference">&$crypto_strong</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   Generates a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> of pseudo-random bytes, with the number of bytes
   determined by the <em><code class="parameter">length</code></em> parameter. 
  </p>
  <p class="para">
   It also indicates if a cryptographically strong algorithm was used to produce the
   pseudo-random bytes, and does this via the optional <em><code class="parameter">crypto_strong</code></em>
   parameter. It&#039;s rare for this to be <strong><code>FALSE</code></strong>, but some systems may be broken or old.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.openssl-random-pseudo-bytes-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">length</code></em></dt>

     <dd>

      <p class="para">
       The length of the desired string of bytes. Must be a positive integer. PHP will
       try to cast this parameter to a non-null integer to use it. 
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">crypto_strong</code></em></dt>

     <dd>

      <p class="para">
       If passed into the function, this will hold a <span class="type"><a href="language.types.boolean.html" class="type boolean">boolean</a></span> value that determines
       if the algorithm used was &quot;cryptographically strong&quot;, e.g., safe for usage with GPG, 
       passwords, etc. <strong><code>TRUE</code></strong> if it did, otherwise <strong><code>FALSE</code></strong>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.openssl-random-pseudo-bytes-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the generated <span class="type"><a href="language.types.string.html" class="type string">string</a></span> of bytes on success,  或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.openssl-random-pseudo-bytes-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-842">
    <p><strong>Example #1 <span class="function"><strong>openssl_random_pseudo_bytes()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">openssl_random_pseudo_bytes</span><span style="color: #007700">(</span><span style="color: #0000BB">$i</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$hex&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">$bytes</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Lengths:&nbsp;Bytes:&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #DD0000">&nbsp;and&nbsp;Hex:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$hex</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$cstrong</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Lengths: Bytes: -1 and Hex: 0
string(0) &quot;&quot;
NULL

Lengths: Bytes: 0 and Hex: 0
string(0) &quot;&quot;
NULL

Lengths: Bytes: 1 and Hex: 2
string(2) &quot;42&quot;
bool(true)

Lengths: Bytes: 2 and Hex: 4
string(4) &quot;dc6e&quot;
bool(true)

Lengths: Bytes: 3 and Hex: 6
string(6) &quot;288591&quot;
bool(true)

Lengths: Bytes: 4 and Hex: 8
string(8) &quot;ab86d144&quot;
bool(true)

</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.openssl-random-pseudo-bytes-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="function.bin2hex.html" class="function" rel="rdfs-seeAlso">bin2hex()</a> - 将二进制数据转换成十六进制表示</span></li>
   <li class="member"><span class="function"><a href="function.crypt.html" class="function" rel="rdfs-seeAlso">crypt()</a> - 单向字符串散列</span></li>
   <li class="member"><span class="function"><a href="function.mt-rand.html" class="function" rel="rdfs-seeAlso">mt_rand()</a> - 生成更好的随机数</span></li>
   <li class="member"><span class="function"><a href="function.uniqid.html" class="function" rel="rdfs-seeAlso">uniqid()</a> - 生成一个唯一ID</span></li>
  </ul>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-public-encrypt.html">openssl_public_encrypt</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.openssl-seal.html">openssl_seal</a></div>
 <div class="up"><a href="ref.openssl.html">OpenSSL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
